from sqlalchemy import Column, Integer, String, Text, Boolean, DateTime, ForeignKey
from sqlalchemy.orm import relationship
from datetime import datetime
from database.db import Base
from .task_scenario_detection_target import task_scenario_detection_targets


class DetectionTarget(Base):
    """检测目标表"""
    __tablename__ = "detection_targets"
    
    id = Column(Integer, primary_key=True, index=True, comment="检测目标ID")
    name = Column(String(200), nullable=False, comment="目标名称")
    description = Column(Text, comment="目标描述")
    target_type = Column(String(50), nullable=False, comment="目标类型")
    properties = Column(Text, comment="目标属性，JSON格式存储")
    is_active = Column(Boolean, default=True, comment="是否激活")
    created_at = Column(DateTime, default=datetime.now, comment="创建时间")
    updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now, comment="更新时间")
    
    # 关系
    task_scenarios = relationship("TaskScenario", secondary="task_scenario_detection_targets", back_populates="detection_target_objects")
    
    def __repr__(self):
        return f"<DetectionTarget(id={self.id}, name='{self.name}', type='{self.target_type}')>"